#coding=utf-8
from uliweb.form import *
from uliweb.i18n import ugettext_lazy as _

class TaskDevelopForm(Form):
    def form_validate(self, data):
        error = {}
        
        y1 = data['begin_time'].year, data['begin_time'].month
        y2 = data['end_time'].year, data['end_time'].month
        if y1 != y2:
            error['end_time'] = '结束日期需要与开始日期为相同的年/月'
        if data['newstage_begin_time']<data['end_time']:
            error['newstage_begin_time'] =  '下一阶段开始时间不能小于上一阶段结束时间'
        if data['end_time']<data['begin_time']:
            error['end_time'] = '上一阶段结束时间不能小于上一阶段开始时间'
        
        return error

class TaskDevelopUpdateForm(Form):
    def form_validate(self, data):
        error = {}
        
        y1 = data['begin_time'].year, data['begin_time'].month
        y2 = data['end_time'].year, data['end_time'].month
        if y1 != y2:
            error['end_time'] = '结束日期需要与开始日期为相同的年/月'
        if data['newstage_begin_time']<data['end_time']:
            error['newstage_begin_time'] =  '下一阶段开始时间不能小于上一阶段结束时间'
        if data['end_time']<data['begin_time']:
            error['end_time'] = '上一阶段结束时间不能小于上一阶段开始时间'
        
        return error

class TaskDetailsUpdateForm(Form):
    def form_validate(self, data):
        error = {}
        
        y1 = data['begin_time'].year, data['begin_time'].month
        y2 = data['end_time'].year, data['end_time'].month
        if y1 != y2:
            error['end_time'] = '结束日期需要与开始日期为相同的年/月'
        if data['end_time']<data['begin_time']:
            error['end_time'] = '上一阶段结束时间不能小于上一阶段开始时间'
        
        return error

class TaskAddForm(Form):
    def form_validate(self, data):
        from uliweb import settings
        
        error = {}
        
        if data.get('task_type') in ('06','15','16') and not data.get('requirement'):
            error['requirement'] = '当任务类型选择配合性测试任务、功能性测试任务、非功能测试任务等任务时，准开发需求为必选项'
        if data.get('task_type') in [x[0] for x in settings.get_var('PARA/TASK_TYPE_OTHER')] and not data.get('task_sys'):
            error['task_sys'] = '此类任务需要选择对应的系统'
        if data.get('task_type') in [x[0] for x in settings.get_var('PARA/TASK_TYPE_PUBLIC')] and data.get('task_sys'):
            error['task_sys'] = '此类任务不需要选择对应的系统'    
        return error

class TaskEditForm(Form):
    def form_validate(self, data):
        from uliweb import settings
        
        error = {}
        
        if data.get('task_type') in [x[0] for x in settings.get_var('PARA/TASK_TYPE_OTHER')] and not data.get('task_sys'):
            error['task_sys'] = '此类任务需要选择对应的系统'
        if data.get('task_type') in [x[0] for x in settings.get_var('PARA/TASK_TYPE_PUBLIC')] and data.get('task_sys'):
            error['task_sys'] = '此类任务不需要选择对应的系统'    
        return error

class TaskAddDevelopForm(Form):
    def form_validate(self, data):
        from uliweb.orm import get_model
        from uliweb import settings
        
        error = {}
        
        Requirement = get_model('requirement')
        System = get_model('system')
        req = data['requirement']
        obj = Requirement.get(req)
        sys = System.get(data['task_sys'])
        
        #判断，如果是项目类任务，则资源来源任务书不能为空
        if data.get('task_type') in settings.get_var('PARA/TASK_TYPE_JUDGE_ASSIGNMENT', []):
            if not data.get('assignment') and data.get('stage', None) != '00':
                error['assignment'] = '任务类型为：项目/子项目/非项目时，任务书不能为空。对于任务书未达时，请先选择无任务书开发任务，待任务书下达后再修改为合适的方式'
        #如果准开发需求为 04-非投产事项任务时，只允许选择 ('25', '非投产重要任务')
#        if obj.mode == '04' and data.get('task_type') != '25':
#            error['task_type'] = '准开发需求为非投产事项任务，所以任务类型只能是"非投产重要任务"'
#        if obj.mode != '04' and data.get('task_type') == '25':
#            error['task_type'] = '准开发需求为投产类任务，所以任务类型不能是"非投产重要任务"'
        return error

class TaskDevelopEditForm(Form):
    def form_validate(self, data):
        from uliweb.orm import get_model
        from uliweb import settings
        
        error = {}
        
#        checkes = [('test_level', '请选择测试级别'),
#            ('test_beginDate', '请输入SIT测试开始时间'),
#            ('test_endDate', '请输入SIT测试结束时间'),
#            ('version_date', '请输入版本投产时间'),
#        ]
        #20110112增加判断任务状态为延迟，延迟原因分类必须选择
        #20110610因为增加了问题和风险管理，所以这块处理取消
#        if data.get('status') == '2' and not data.get('delay_classes'):
#            error['delay_classes'] = '当前任务已经延迟，延迟原因分类不能为空'
        #判断是否主系统
#        if not self.obj.version_headerId:
#            #主系统必须要输入相应的里程碑时间
#            for f, msg in checkes:
#                if not data.get(f):
#                    error[f] = msg
        
        #判断，如果是项目类任务，则资源来源任务书不能为空
        if data.get('task_type') in settings.get_var('PARA/TASK_TYPE_JUDGE_ASSIGNMENT', []):
            if not data.get('assignment') and self.object.stage != '00':
                error['assignment'] = '任务类型为：项目/子项目/非项目时，任务书不能为空。对于任务书未达时，请先选择无任务书开发任务，待任务书下达后再修改为合适的方式'
#        if self.object.requirement.mode == '04' and data.get('task_type') != '25':
#            error['task_type'] = '准开发需求为非投产事项任务，所以任务类型只能是"非投产重要任务"'
#        if self.object.requirement.mode != '04' and data.get('task_type') == '25':
#            error['task_type'] = '准开发需求为投产类任务，所以任务类型不能是"非投产重要任务"'
        return error

class TaskMilestoneEditForm(Form):
    def form_validate(self, data):
        error = {}
        
        if data.get('finish_date') and data.get('finish_date') > datetime.date.today():
            error['finish_date'] = '完成日期不能大于当前系统日期(%s)' % datetime.date.today()
            
        return error